package com.sita.tboard.util;

/**
 * Created by xiaodong on 15/11/6.
 */
public class DistanceUtil {
    private static final double EARTH_RADIUS = 6371000;//赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 基于googleMap中的算法得到两经纬度之间的距离,计算精度与谷歌地图的距离精度差不多，相差范围在0.2米以下
     *
     * @param longitude1 第一点的经度
     * @param latitude1 第一点的纬度
     * @param longitude2 第二点的经度
     * @param latitude2 第二点的纬度
     * @return 返回的距离，单位m
     */
    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        double radLat1 = rad(latitude1);
        double radLat2 = rad(latitude2);
        double a = radLat1 - radLat2;
        double b = rad(longitude1) - rad(longitude2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    public static double getDistance(String longitude1, String latitude1, String longitude2, String latitude2) {
        double radLat1 = rad(Double.valueOf(latitude1));
        double radLat2 = rad(Double.valueOf(latitude2));
        double a = radLat1 - radLat2;
        double b = rad(Double.valueOf(longitude1)) - rad(Double.valueOf(longitude2));
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    /*
     * 二维数组排序，比较array[][2]的值，返回二维数组
     * */
    public static String[][] getOrder(String[][] array){
        for (int j = 0; j < array.length ; j++) {
            for (int bb = 0; bb < array.length - 1; bb++) {
                String[] ss;
                int a1=Integer.valueOf(array[bb][2]);  //转化成int型比较大小
                int a2=Integer.valueOf(array[bb+1][2]);
                if (a1>a2) {
                    ss = array[bb];
                    array[bb] = array[bb + 1];
                    array[bb + 1] = ss;
                }
            }
        }
        return array;
    }

    /*打印数组*/
    public static void showArray(String[][] array){
        for(int a=0;a<array.length;a++){
            for(int j=0;j<array[0].length;j++)
                System.out.print(array[a][j]+" ");
            System.out.println();
        }
    }
}
